﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
//using System.Threading.Tasks;
using System.Web.Security;
using Microsoft.SharePoint;
using System.Web;
using Microsoft.SharePoint.Administration.Claims;
namespace ContosoProviders
{
    class MemberShipProviderData
    {
        /// <summary>
        /// MembershipUser(string providerName, string name, object providerUserKey, string email, string passwordQuestion, string comment, bool isApproved, bool isLockedOut, DateTime creationDate, 
        /// DateTime lastLoginDate, DateTime lastActivityDate, DateTime lastPasswordChangedDate, DateTime lastLockoutDate);
        /// </summary>
        /// <returns></returns>
        internal MembershipUserCollection FindUsersByEmail(string emailMatch)
        {
            string sqlQuery = "SELECT EmployeeNumber, EmailAddress FROM Gtw.Employee WHERE EmailAddress = @EmailAddress;";

            SqlParameter[] parms = new SqlParameter[] {
					new SqlParameter("@EmailAddress",emailMatch)};

            MembershipUserCollection users = FillUsers(sqlQuery, parms);

            return users;
        }


        internal MembershipUserCollection FindUsersByName(string usernameToMatch)
        {
            string sqlQuery = "SELECT EmployeeNumber, EmailAddress FROM Gtw.Employee WHERE EmployeeNumber = @EmployeeNumber;";

            SqlParameter[] parms = new SqlParameter[] {
					new SqlParameter("@EmployeeNumber",usernameToMatch)};
            
            MembershipUserCollection users = FillUsers(sqlQuery, parms);

            return users;
        }

        internal MembershipUserCollection GetAllUsers()
        {
            string sqlQuery = "SELECT EmployeeNumber, EmailAddress FROM Gtw.Employee";

            MembershipUserCollection users = FillUsers(sqlQuery, null);

            return users;
        }

        internal MembershipUser GetUser(string username)
        {
            MembershipUserCollection users = FindUsersByName(username);

            return users[username];
        }

        internal string GetUserNameByEmail(string email)
        {
            string userName = string.Empty;

            MembershipUserCollection users = FindUsersByEmail(email);

            foreach (MembershipUser user in users)
            {
                if (user.Email == email)
                {
                    userName = user.Email;
                }
            }

            return userName;
        }

        internal bool ValidateUser(string username, string password)
        {
            string sqlQuery = "SELECT EmployeeNumber, EmailAddress FROM Gtw.Employee where EmployeeNumber=@EmployeeNumber and Password=@Password";

            SqlParameter[] parms = new SqlParameter[] {
					new SqlParameter("@EmployeeNumber",username),
					new SqlParameter("@Password", password)};

            MembershipUserCollection users = FillUsers(sqlQuery, parms);

            if (users != null && users.Count > 0)
                return true;

            return false;
        }

        internal bool ChangePassword(string username, string newPassword)
        {
            string sqlQuery = "UPDATE Gtw.Employee SET Password=@Password WHERE EmployeeNumber=@EmployeeNumber";

            SqlParameter[] parms = new SqlParameter[] {
					new SqlParameter("@EmployeeNumber",username),
					new SqlParameter("@Password", newPassword)};

            int count = SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, CommandType.Text, sqlQuery, parms);

            if (count > 0)
                return true;
            else
                return false;
        }


        internal object CreateUser(string username, string password, string email)
        {
            SqlParameter[] parms = new SqlParameter[] {
					new SqlParameter("@EmployeeNumber",username),
					new SqlParameter("@Password", password),
                    new SqlParameter("@EmailAddress", email)
            };

            return SqlHelper.ExecuteScalar(SqlHelper.ConnectionString, CommandType.StoredProcedure, "SP_CreateUser", parms);
        }
        
        /// <summary>
        /// Fill Membership users based on param value
        /// </summary>
        /// <param name="sqlQuery"></param>
        /// <param name="parm"></param>
        /// <returns></returns>
        private MembershipUserCollection FillUsers(string sqlQuery, SqlParameter[] parm)
        {
            MembershipUserCollection users = new MembershipUserCollection();

            //Execute a query to read the users
            using (SqlDataReader rdr = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, CommandType.Text, sqlQuery, parm))
            {
                while (rdr.Read())
                {
                    users.Add(new MembershipUser(
                        Membership.Provider.Name,
                        rdr.GetString(0),
                        rdr.GetString(0),
                        rdr.GetString(1),
                        string.Empty,
                        string.Empty,
                        true,
                        false,
                        DateTime.Today,
                        DateTime.Today,
                        DateTime.Today,
                        DateTime.Today,
                        DateTime.Today));
                }
            }

            return users;
        }

        private DateTime GetDateValue(DateTime? dateTime)
        {
            if (dateTime != null && dateTime.HasValue)
                return Convert.ToDateTime(dateTime);
            else
                return DateTime.MinValue;
        }
        
    }
}
